<?php
class CategoryController extends AdminController{
    public function indexAction(){
        // Yaf_Dispatcher::getInstance()->disableView();
        
        $model = new CategoryModel;
        $data = $model->get_tree();
        // var_dump($data);
        $this->_view->data = $data;
    }
    public function createAction(){
        $model = new CategoryModel;
        $data = $model->select();
        // var_dump($data);
        $this->_view->data = $data;
    }
    public function detree($arg,$level = 0){
        $px = ($level*20).'px';
        $level = $level+1;
        foreach($arg as $value){
            $html = '<tr>';
            $html .= '<td>'.$value['id'].'</td>';
            $html .= '<td><p style="padding-left:'.$px.'">'.$value['name'].'</p></td>';
            $html .= '<tr>';
            if(isset($value['_'])){
                $this->detree($value['_'],$level);
            }
        }
        return $html;
    }
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $data['fid'] = Request::aspost(3,'fid',0);
        $data['name'] = Request::aspost(6,'name','');
        $data['sort'] = Request::aspost(3,'sort','');
        $data['link'] = Request::aspost(6,'url','');
        $data['description'] = Request::aspost(6,'description','');
        $data['image'] = Request::aspost(6,'image','');
        $model = new CategoryModel;
        $flag = $model->insert($data);
        if(empty($flag)){
            Response::error('Error');
        }else{
            Response::success('Success');
        }
    }
    public function editAction(){
        $id = Request::asget(3,'id',null);
        if($id == null){
            return false;
        }else{
            $where = ['id'=>$id];
            $model = new CategoryModel;
            $categorys = $model->select();
            $data = [];
            foreach($categorys as $key=>$cate){
                if($cate['id'] == $id){
                    $data = $cate;
                    unset($categorys[$key]);
                    break;
                }
            }
            $this->_view->categorys = $categorys;
            $this->_view->data = $data;
        }
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if($id == null){
            return false;
        }else{
            $data['fid'] = Request::aspost(3,'fid',0);
            $data['name'] = Request::aspost(6,'name','');
            $data['sort'] = Request::aspost(3,'sort','');
            $data['link'] = Request::aspost(6,'link','');
            $data['description'] = Request::aspost(6,'description','');
            $data['image'] = Request::aspost(6,'image','');
            $model = new CategoryModel;
            $flag = $model->where(['id'=>$id])->update($data);
            if(empty($flag)){
                Response::error('Error');
            }else{
                Response::success('Success');
            }            
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            // $status = Request::aspost(3,'status','');
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if($status == ''){
                return false;
            }else{
                
                switch($status){
                case '0': $data['status'] = 1;
                    break;
                case '1': $data['status'] = 0;
                    break;
                }
                $model = new CategoryModel;
                $flag = $model->where(['id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('');
                }else{
                    Response::success('nice');
                }
            }
        }        
    }

    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id',null);
        if(null == $id){
            die;
        }else{
            $model = new CategoryModel;
            $flag = $model->where(['id' => $id])->delete();
            // $flag = 0;
            if($flag == 0){
                return Response::success('nice');
                
            }else{
                return Response::error('error');
            }            
        }

    }
    public function upfileAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $up = new Upload;
        $up->save_path = 'category';
        $file = $up->upload();
        Response::work($file);
    }
}